package com.hhc.netty;

import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 保存移动端和设备终端信息
 */
public class NettySocketChannelMap {
    private static Map<String, SocketChannel> clientMap = new ConcurrentHashMap<>();
    private static Map<String, SocketChannel> deviceMap = new ConcurrentHashMap<>();

    public static void addClient(String clientId, SocketChannel socketChannel) {
        clientMap.put(clientId, socketChannel);
    }

    public static void addDevice(String deviceId, SocketChannel socketChannel) {
        deviceMap.put(deviceId, socketChannel);
    }

    public static Channel getClient(String clientId) {
        return clientMap.get(clientId);
    }

    public static Channel getDevice(String deviceId) {
        return deviceMap.get(deviceId);
    }


    public static void remove(SocketChannel socketChannel) {
        for (Map.Entry entry : clientMap.entrySet()) {
            if (entry.getValue() == socketChannel) {
                clientMap.remove(entry.getKey());
            }
        }

        for (Map.Entry entry : deviceMap.entrySet()) {
            if (entry.getValue() == socketChannel) {
                deviceMap.remove(entry.getKey());
            }
        }
    }
}
